<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
    <?php $imageBlock =  $this->getLayout()->createBlock('Magento\Catalog\Block\Product\Image');?>
    <div class="block widget new minilist">
        <div class="title">
            <strong><?php echo __('New Products') ?></strong>
        </div>
        <div class="content">
            <?php echo $this->getPagerHtml() ?>
            <?php $suffix = $this->getNameInLayout(); ?>
            <?php $iterator = 1; ?>
            <ol class="items minilist products" id="widget-new-products-<?php echo $suffix; ?>">
                <?php foreach ($_products->getItems() as $_product): ?>
                    <?php echo ($iterator++==1) ? '<li class="item product">' : '</li><li class="item product">' ?>
                    <div class="product">
                        <a class="product photo" href="<?php echo $_product->getProductUrl() ?>"
                           title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
                            <?php echo $imageBlock->init($_product, 'side_column_widget_product_thumbnail')->toHtml() ?>
                        </a>
                        <div class="product details">
                            <strong class="product name">
                                <a href="<?php echo $_product->getProductUrl() ?>"
                                   title="<?php echo $this->stripTags($_product->getName(), null, true) ?>)">
                                    <?php echo $this->helper('Magento\Catalog\Helper\Output')->productAttribute($_product, $_product->getName(), 'name') ?>
                                </a>
                            </strong>
                            <?php echo $this->getProductPriceHtml($_product, '-widget-new-' . $suffix) ?>
                            <div class="product actions">
                                <div class="primary">
                                    <?php if($_product->isSaleable()): ?>
                                        <?php if ($_product->getTypeInstance()->hasRequiredOptions($_product)): ?>
                                            <button type="button" title="<?php echo __('Add to Cart') ?>" class="action tocart"
                                                    data-mage-init='{"redirectUrl":{"url":"<?php echo $this->getAddToCartUrl($_product) ?>"}}'>
                                                <span><?php echo __('Add to Cart') ?></span>
                                            </button>
                                        <?php else: ?>
                                            <?php
                                                $postDataHelper = $this->helper('Magento\Core\Helper\PostData');
                                                $postData = $postDataHelper->getPostData($this->getAddToCartUrl($_product), ['product' => $_product->getEntityId()]);
                                        ?>
                                        <button type="button" title="<?php echo __('Add to Cart') ?>" class="action tocart"
                                                data-post='<?php echo $postData; ?>'>
                                            <span><?php echo __('Add to Cart') ?></span>
                                        </button>
                                    <?php endif; ?>
                                    <a href="<?php echo $this->getAddToCartUrl($_product) ?>" class="action tocart">
                                            <span><?php echo __('Add to Cart') ?></span>
                                        </a>
                                    <?php else: ?>
                                        <?php if ($_product->getIsSalable()): ?>
                                            <p class="stock available" title="<?php echo __('Availability') ?>">
                                                <span><?php echo __('In stock') ?></span>
                                            </p>
                                        <?php else: ?>
                                            <p class="stock unavailable" title="<?php echo __('Availability') ?>">
                                                <span><?php echo __('Out of stock') ?></span>
                                            </p>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php echo ($iterator==count($_products->getItems())+1) ? '</li>' : '' ?>
                <?php endforeach; ?>
            </ol>
        </div>
    </div>
<?php endif; ?>
